package com.itheima.web.listener;

import cn.hutool.core.util.ArrayUtil;
import com.itheima.autoconfig.huawei.HuaWeiUGCProperties;
import com.itheima.autoconfig.huawei.HuaWeiUGCTemplate;
import com.itheima.domain.mongo.Movement;
import com.itheima.service.mongo.MovementService;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
@RocketMQMessageListener(consumerGroup = "movement-group", topic = "tanhua-movement")
public class MovementCheckListener implements RocketMQListener<String> {

    @Reference
    private MovementService movementService;

    @Autowired
    private HuaWeiUGCTemplate weiUGCTemplate;

    @Override
    public void onMessage(String movementId) {
        System.out.println("动态id：" + movementId);

        // 查询动态详情
        Movement movement = movementService.findMovementById(new ObjectId(movementId));

        // 审核文本
        boolean textCheck = weiUGCTemplate.textContentCheck(movement.getTextContent());
        // 审核图片
        boolean imageCheck = weiUGCTemplate.imageContentCheck(movement.getMedias().toArray(new String[movement.getMedias().size()]));


        if (textCheck&&imageCheck ){ // 通过
            movement.setState(1);
        }else{ // 不通过
            movement.setState(2);
        }

        // 将审核结果同步到mongo中
        movementService.updateMovement(movement);

    }
}
